<?php
/**
 * Created by PhpStorm.
 * User: 86188
 * Date: 2021/7/18
 * Time: 14:07
 */

namespace app\wxpro\controller;

use app\service\imageService;
use think\facade\Db;
use app\BaseController;
class Visitor extends BaseController
{

    public function visitorList(){
        $openid = request()->param("openid");
        $visitorlist = Db::table("gatevisitor")
            ->where("user_status",1)
            ->where("user_type",2)
            ->where("delete_status",0)
            ->where("from_source_openid",$openid)
            ->order("id desc")
            ->select();
        return json_encode(["errCode"=>"0",'visitorlist' =>$visitorlist]);
    }
    public function visitorAdd(){
        $openid = request()->param("openid");
        $name = request()->param("name");
        $phone = request()->param("phone");
        $wxuserinfo =Db::table("wxuser")->where("openid",$openid)->find();
        if(empty($wxuserinfo)){
            return json_encode(["errCode"=>"1",'errormsg' =>"添加失败"]);
        }
        $info["from_source_openid"] = $openid;
        $info["from_source_wxuserid"] = $wxuserinfo["id"];
        $info["wiegand"] = $wxuserinfo["wiegand"];
        $info["phone"] = $phone;
        $info["name"] = $name;
        $info["check_status"] = 1;
        $info["user_type"] = 2;
        $info["community_id"] = $wxuserinfo["community_id"];
        //$info["community_name"] = $wxuserinfo["community_name"];
        $info["create_time"] = $info["update_time"] = time();

        $id = Db::table("gatevisitor")->insertGetId($info);
        if($id){
            return json_encode(["errCode"=>"0",'visitor_id' =>$id]);
        }else{
            return json_encode(["errCode"=>"1",'errormsg' =>"添加失败"]);
        }
    }

    public function visitorinfo(){
        $visitor_id = request()->param("visitor_id");
        $visitorinfo = Db::table("gatevisitor")
            ->where("id",$visitor_id)
            ->find();
        if(empty($visitorinfo)){
            return json_encode(["errCode"=>"1",'errormsg' => '用户信息获取失败']);
        }

        if($visitorinfo["check_status"]== '0'){
            return json_encode(["errCode"=>"1",'errormsg' => '用户信息未审核']);
        }
        if(empty($visitorinfo["ewm"])){

            $resewm=(new imageService())->createimg($visitorinfo["id"]);
            if($resewm)
            {
                $ewm="https://www.zz.zfbzhsq.com/static/ewm/".$visitorinfo["id"].".png";
                $visitorinfo["ewm"] = $ewm;
                DB::table("gatevisitor")->where("id",$visitorinfo["id"])->update(["ewm"=>$ewm]);
            }else
            {
                return json(["errCode"=>"1",'errormsg' => "二维码生成失败"]);
            }
        }
        $data["visitorinfo"]=  $visitorinfo;
        return json_encode(["errCode"=>"0",'datainfo' =>$data]);
    }
}